package org.gensokyo.crawler.luoli

import java.util.UUID

import com.gargoylesoftware.htmlunit._
import com.gargoylesoftware.htmlunit.html._
import com.typesafe.scalalogging.slf4j.StrictLogging
import org.apache.commons.codec.digest.DigestUtils
import org.gensokyo.crawler.proxy.ProxyInfo
import org.json4s.jackson.Serialization._

import scala.collection.Map
import scala.collection.parallel.ForkJoinTaskSupport


object LulubaHandler extends StrictLogging {

  import org.json4s._
  import org.json4s.jackson.JsonMethods._
  import org.json4s.jackson.Serialization._

  import scalaj.collection.Imports._

  implicit val formats = org.json4s.DefaultFormats


  def handle(proxyIp: String, proxyPort: Int) = {
    var page: HtmlPage = null
    var webClient: WebClient = null
    try {
      logger.debug("start")
      webClient = new WebClient(BrowserVersion.FIREFOX_45, "221.193.235.94", 9999)

      webClient.getOptions.setThrowExceptionOnScriptError(false)
      webClient.getOptions.setThrowExceptionOnFailingStatusCode(false)
      webClient.setConfirmHandler(new ConfirmHandler() {
        def handleConfirm(page: Page, message: String): Boolean = {
          logger.debug(s"----------------------------------------")
          logger.debug(s"confirm message:${message}")
          logger.debug(s"----------------------------------------")
          true
        }
      })
      webClient.setAlertHandler(new AlertHandler() {
        override def handleAlert(page: Page, message: String): Unit = {
          logger.debug(s"----------------------------------------")
          logger.debug(s"alert message:${message}")
          logger.debug(s"----------------------------------------")
        }
      })
      webClient.getOptions.setJavaScriptEnabled(false)
      webClient.setJavaScriptTimeout(10L)
      val url1 = "http://www.avshe8.com/?x=347989"
      val url2 = "http://www.avshe8.com/?fromuid=372227"
      val page1: HtmlPage = webClient.getPage(url2)
      logger.debug(s"cookies:${webClient.getCookieManager.getCookies.asScalaMutable.toList}")
      val page2: HtmlPage = webClient.getPage("http://www.avshe8.com/forum.php?mod=ajax&inajax=yes&infloat=register&handlekey=register&ajaxmenu=1&action=checkemail&email=srzyhead002@qq.com")
      logger.debug(s"cookies:${webClient.getCookieManager.getCookies.asScalaMutable.toList}")
      //      val requestSettings = new WebRequest(url, HttpMethod.POST);
      val page3: HtmlPage = webClient.getPage("http://www.avshe8.com/forum.php?mod=ajax&inajax=yes&infloat=register&handlekey=register&ajaxmenu=1&action=checkemail&email=srzyhead002@qq.com")
      //      curl 'http://www.avshe8.com/member.php?mod=register&inajax=1' -H 'Pragma: no-cache' -H 'Cookie: JGYP_2132_saltkey=loU3WzM5; JGYP_2132_lastvisit=1486989818; JGYP_2132_sid=C9zkUK; JGYP_2132_lastact=1487000571%09forum.php%09ajax' -H 'Origin: http://www.avshe8.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36' -H 'Proxy-Authorization: Basic c3J6eWhlYWQ6aWRjOTAwMF9jb20=' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryrKnhITXneblQ4O20' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: no-cache' -H 'Referer: http://www.avshe8.com/member.php?mod=register' -H 'Proxy-Connection: keep-alive' --data-binary $'------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="regsubmit"\r\n\r\nyes\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="formhash"\r\n\r\n6210ecb9\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="referer"\r\n\r\nhttp://www.avshe8.com/./\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="activationauth"\r\n\r\n\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="ijuOQY"\r\n\r\nsrzyhead001\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="asu1b8"\r\n\r\nsrzyhead001\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="IsgzQO"\r\n\r\nsrzyhead001\r\n------WebKitFormBoundaryrKnhITXneblQ4O20\r\nContent-Disposition: form-data; name="n84z48"\r\n\r\nsrzyhead001@qq.com\r\n------WebKitFormBoundaryrKnhITXneblQ4O20--\r\n' --compressed

      //      val page2: HtmlPage = webClient.getPage("http://www.avshe8.com/member.php?mod=register")
      logger.debug(s"1111")

      //      scala.tools.nsc.io.File("1.xml").writeAll(xml)
      //      scala.tools.nsc.io.File("tfkj_exam_公司制度培训.json").writeAll(pretty(render(Extraction.decompose(examInfo))))

      //      logger.debug(s"ll:${ll}")
      logger.debug("deal over")
    } catch {
      case e: Exception => logger.error(e.getMessage, e)
    } finally {
      webClient.close()
    }
  }

  def deal = {
    handle("218.18.16.44", 8998)
  }

  def main(args: Array[String]): Unit = {
    deal
  }

}
